//
//  KPChooseLocalViewController.swift
//  KPBaseProject
//
//  Created by 孑孓 on 2018/4/3.
//  Copyright © 2018年 Kipp. All rights reserved.
//

import UIKit

class KPChooseLocalViewController: KPBaseViewController, UITableViewDelegate, UITableViewDataSource {

    var cityListArray : [String] = [String]()
    var tableView:UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        createUI()
    }

    func createUI() {
        self.title = "选择城市"
        getData()

        tableView = UITableView.init(frame: CGRect.init(x: 0, y: 0, width: Screen.width, height: Screen.height - kNavibarH), style: UITableViewStyle.plain)
        tableView.backgroundColor = UIColor.white
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(ChooseLocalCell.self, forCellReuseIdentifier: "ChooseLocalCell")
        self.view.addSubview(tableView)


    }

    func getData() {
        let path = Bundle.main.path(forResource: "citys", ofType: ".json")
        let jsonData = NSData(contentsOfFile: path!)
        let obj = try! JSONSerialization.jsonObject(with: (jsonData as! NSData) as Data, options: .mutableContainers) as! NSArray

        let cityS = NSMutableArray()
        for i in 0..<obj.count {
            let dic = obj[i] as! NSDictionary
            let cittList = dic.object(forKey: "citylist") as! NSArray
            cityS.addObjects(from: cittList as! [Any])
        }

        var arr1:[String] = [String]()
        for i in 0..<cityS.count {
            let dic = cityS[i] as! NSDictionary
            arr1.append(dic.object(forKey: "name") as! String)
        }

        cityListArray = arr1.sortedByPinyin()!
//        for i in 0..<cityListArray.count{
//            let s = getFirCharactor(str: cityListArray[i])
//            KPLog(s)
//        }
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return cityListArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ChooseLocalCell") as! ChooseLocalCell
        cell.localLa?.text = cityListArray[indexPath.row]
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }

    func getFirCharactor(str: String) -> String {
        let mString = NSMutableString(string: str)
        CFStringTransform(mString, nil, kCFStringTransformToLatin, false)
        CFStringTransform(mString, nil, kCFStringTransformStripDiacritics, false)
        let string = mString.capitalized
        return string.substring(to: string.index(string.startIndex, offsetBy: 1))
    }
}

extension Array {
    func sortedByPinyin(ascending: Bool = true) -> Array<String>? {
        if self is Array<String> {
            return (self as! Array<String>).sorted { (value1, value2) -> Bool in
                let pinyin1 = value1.transformToPinYin()
                let pinyin2 = value2.transformToPinYin()
                return pinyin1.compare(pinyin2) == (ascending ? .orderedAscending : .orderedDescending)
            }
        }
        return nil
    }
    func filterDuplicates<E: Equatable>(_ filter: (Element) -> E) -> [Element] {
        var result = [Element]()
        for value in self {
            let key = filter(value)
            if !result.map({filter($0)}).contains(key) {
                result.append(value)
            }
        }
        return result
    }
}
